//
//  GestureViewModel.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/30.
//

import UIKit
import RxSwift
import RxCocoa
import CRPEarPhone

public typealias FoldAction = (mode: CRPButtonMode, isOpen: Bool)
public typealias ModeFunc = (mode: CRPButtonMode, funcType: CRPButtonFuncType)

public class GestureViewModel: ViewModel, ViewModelType {
    
    let eartype: CRPEarType
    
    public private(set) var dataSource: [GestureSectionModel] = []
    
    public init(eartype: CRPEarType) {
        self.eartype = eartype
        super.init()
    }
    
    public struct Input {
        public let fold: Observable<FoldAction>
        public let selectItem: Observable<ModeFunc>
        
        public init(fold: Observable<FoldAction>, selectItem: Observable<ModeFunc>) {
            self.fold = fold
            self.selectItem = selectItem
        }
    }
    
    public struct Output {
        public let sections: Observable<[GestureSectionModel]>
    }

    public func transform(input: Input) -> Output {
        
        let sections = BehaviorRelay<[GestureSectionModel]>(value: [])
        sections.subscribe(onNext: { [weak self] value in
            guard let `self` = self else { return }
            self.dataSource = value
        }).disposed(by: rx.disposeBag)
        
        input.fold
            .map { [weak self] mode, isOpen in
                guard let `self` = self else { return [] }
                if let index = self.dataSource.firstIndex(where: { $0.buttonMode == mode }) {
                    var section = self.dataSource[index]
                    if isOpen {
                        section.items = section.supprotFuncTypes.map{ GestureCellViewModel(bunntonMode: section.buttonMode, buttonFuncType: $0) }
                        section.selectFuncType = section.selectFuncType
                    } else {
                        section.items = []
                    }
                    self.dataSource[index] = GestureSectionModel(original: section, items: section.items)
                }
                return self.dataSource
            }.bind(to: sections).disposed(by: rx.disposeBag)
        
        input.selectItem
            .subscribe(onNext: { [weak self] mode, funcType in
                guard let `self` = self else { return }
                let model = CRPButtonFuncModel(earType: self.eartype,
                                                       buttonMode: mode,
                                                       buttonFuncType: funcType)
                BluetoothManager.shared.sendKeyAction(model: model)
                if let index = self.dataSource.firstIndex(where: { $0.buttonMode == mode }) {
                    var section = self.dataSource[index]
                    section.selectFuncType = funcType
                    self.dataSource[index] = section
                }
            }).disposed(by: rx.disposeBag)
        
        Observable.combineLatest(BluetoothManager.shared.querySupportKeyAction(), BluetoothManager.shared.queryAllKeyAction())
            .map({ [weak self] supportTypeModel, actions in
                guard let `self` = self else { return [] }
                // 做个去重
                let supportType = supportTypeModel.supportType.reduce([], {  $0.contains($1) ? $0 : $0 + [$1]  })
                return supportTypeModel.supportMode.map { mode in
                    var section = GestureSectionModel(eartype: self.eartype,
                                                      buttonMode: mode,
                                                      supprotFuncTypes: supportType,
                                                      items: [] )
                    if let config = actions.first(where: { $0.earType == self.eartype && $0.buttonMode == mode }) {
                        section.selectFuncType = config.buttonFuncType
                    }
                    return section
                }
            }).bind(to: sections).disposed(by: rx.disposeBag)
     
        return Output(sections: sections.asObservable())
    }
}

